package com.bw.user.service.impl;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bw.core.result.ResponseResult;
import com.bw.user.pojo.TbIdentityAuth;
import com.bw.user.mapper.TbIdentityAuthMapper;
import com.bw.user.service.TbIdentityAuthService;
import com.bw.user.vo.IdentityAuthVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.Map;

/**
 * <p>
 * 身份认证表&信息 服务实现类
 * </p>
 *
 * @author lv
 * @since 2021-03-23
 */
@Service
public class TbIdentityAuthServiceImpl extends ServiceImpl<TbIdentityAuthMapper, TbIdentityAuth> implements TbIdentityAuthService {

    @Autowired
    private TbIdentityAuthMapper identityAuthMapper;
    @Override
    public ResponseResult scanidcard(MultipartFile IdCardFront) {

        try {

            String post = HttpUtil.createPost("http://localhost:8080/api/common/ocr")
                    .form("imagePic", IdCardFront.getBytes(), IdCardFront.getOriginalFilename()).execute().body();
            ResponseResult result = JSONObject.parseObject(post, ResponseResult.class);
            return result;

        } catch (Exception e) {
            e.printStackTrace();

        }

        return ResponseResult.error(null);
    }


    @Override
    public String certification(String name, String cardId, MultipartFile IdCardFront, TbIdentityAuthService identityAuthService) {
        try {

            String post = HttpUtil.createPost("http://localhost:8080/api/common/identityAuth")
                    .form("frontPic", IdCardFront.getBytes(), IdCardFront.getOriginalFilename())
                    .form("cardId", cardId)
                    .form("name", name).execute().body();

            return post;

        } catch (Exception e) {
            e.printStackTrace();

        }

        return "失败";
    }

    @Override
    public ResponseResult iditiy(IdentityAuthVo identityAuthVo, Map<String,String> map) {
        TbIdentityAuth tbIdentityAuth = new TbIdentityAuth();
        tbIdentityAuth.setIdentityImg1(map.get("IdCardFront"));
        tbIdentityAuth.setIdentityImg2(map.get("IdCardBank"));
        tbIdentityAuth.setUserImg(map.get("LivePic"));
        tbIdentityAuth.setIdentityNo(identityAuthVo.getCardId());
        tbIdentityAuth.setUserRealName(identityAuthVo.getName());
        tbIdentityAuth.setCreateTime(new Date());
        tbIdentityAuth.setUpdateTime(new Date());
        identityAuthMapper.insert(tbIdentityAuth);
        return ResponseResult.success("认证成功");
    }

    @Override
    public String  livingauthen(MultipartFile livePic) {

        try {

            String post = HttpUtil.createPost("http://localhost:8080/api/common/liveAuth")
                    .form("livePic", livePic.getBytes(), livePic.getOriginalFilename()).execute().body();
            return post;

        } catch (Exception e) {
            e.printStackTrace();

        }

        return "失败";
    }
}
